package com.zl.customer.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局的异常处理器
 * @ClassName: CustomerExceptionHandler
 * @author: suke
 * @since: 2021/8/5 14:20
 */
public class CustomerExceptionHandler implements HandlerExceptionResolver{

    /**
     * 处理异常
     * @param request   请求对象
     * @param response  响应对象
     * @param handler   处理器对象
     * @param ex   抛出异常的对象
     * @return
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();
        if(ex instanceof CustomerException ){
            //如果是自定义异常, 直接显示异常信息
            modelAndView.addObject("msg",ex.getMessage());
        }else{
            //如果是系统异常, 显示 "系统出故障了, 请联系管理员"
            modelAndView.addObject("msg","系统出故障了, 请联系管理员");
        }
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
